<?php


	include_once("cmsConfig.php");
	//include_once("CmsStatusMessage.php");
	

	class CmsFormExport {

		var $formId;
		var $authInfo;
		var $db;


		var $formDescription;
		var $formElements;
		var $availableFields = array();
		var $availableLabels = array();
		var $customFields = array();
		var $customLabels = array();
		
		var $formName;
		
		
		function CmsFormExport( $formId, &$authInfo, &$db) {
			$this->formId = $formId;
			$this->authInfo  = $authInfo;
			$this->db = &$db;
			
			$cmsTableWriter = new CmsTableWriter(CMS_FORM_DESC_FORMAT, $db);
			$this->formDescription = $cmsTableWriter->readItem($this->formId);
			$this->formElements = new CmsFormElement($this->formDescription, $db);
			$this->formName = $this->formDescription->formname;
			
			unset($this->formDescription->formid);
			unset($this->formDescription->formname);
			unset($this->formDescription->usertype);
			unset($this->formDescription->editmode);
			unset($this->formDescription->uid);
			unset($this->formDescription->gid);
			unset($this->formDescription->permission);
			unset($this->formDescription->sidebarmodule);
			unset($this->formDescription->activemodule);
			unset($this->formDescription->contentmodule);
			unset($this->formDescription->modeaccess);
			unset($this->formDescription->displaygroup);
			
			unset($this->formDescription->documenttypecode);
			unset($this->formDescription->categorycode);
			unset($this->formDescription->datelogged);
			unset($this->formDescription->documentdate);
			unset($this->formDescription->documentyear);
			unset($this->formDescription->startdate);
			unset($this->formDescription->enddate);
			//unset($this->formDescription->documentcode);			

			// available fields
			foreach($this->formDescription as $field=>$v){
				if($v)
					array_push($this->availableFields, $field);
			}
			
			// label of available fields
			$this->availableLabels = array();
			foreach($this->availableFields as $field){
				$e = $this->formElements->getElementDescriptor($this->formDescription->$field);
				array_push($this->availableLabels,$e->label);
			}			

			// custom fields
			$this->db->executeQuery("SELECT * FROM columnexport WHERE formid='$this->formId'");
			while ($row = $this->db->nextObject()) {
				if(in_array($row->columntoexport,$this->availableFields)){
					$this->customFields[array_search($row->columntoexport, $this->availableFields)] = $row->columntoexport;
				}
			}
			ksort($this->customFields);

			// label of custom fields
			foreach($this->customFields as $field){
				$e = $this->formElements->getElementDescriptor($this->formDescription->$field);
				array_push($this->customLabels,$e->label);
			}			
		}
		
		function processExport($ext) {
			
			header("Content-type: text/$ext");
			header('Content-Disposition: attachment; filename="Spreadsheet-'.ucfirst($this->formName).'-'.date("Y-m-d").'.'.$ext.'"'."\n");

			if($ext=='csv'){
				# page to generate csv file
				# all fields must be separated by commas.
				# all fields should be wrapped in double quotes
				# all double quotes should be escaped with double quotes
				# the first row will contain field headers
	
				$firstrow = implode('","', $this->customLabels);
				$firstrow = '"' . $firstrow . '"'."\n";
				echo $firstrow;
	
				$cmsTableWriter = new CmsTableWriter(CMS_FORMAT, $this->db);
				$cmsTableWriter->resetCustomIterator(" WHERE formid='".$this->formId."'");
				while ($row = $cmsTableWriter->nextItem()){
					$displayrow = array();
					foreach ($this->customFields as $field){
						
						$e = $this->formElements->getElementDescriptor($this->formDescription->$field);
						if($e->elementtype==CMS_ELEMENT_DROPDOWN){
							$dropDownTableWriter = new CmsTableWriter('cmscontentmetadata', $this->db);
							$dropDownTableWriter->resetCustomIterator(" WHERE documentcode='".$row->$field."' LIMIT 1");
							$dropDown = $dropDownTableWriter->nextItem();
							if(!$dropDown){
								// assuming it's country
								$query = "SELECT name FROM country WHERE id='".$row->$field."'";
								$this->db->executeQuery($query);
								$value = $this->db->nextObject();
								if($value){
									$output = $value->name;
								} else {
									$output = "";
								}	
							} else {
								$output = $dropDown->label;
							}
						}elseif($e->elementtype==CMS_ELEMENT_CHECKBOX_LIST){
							$query = "SELECT c.label FROM cmscontentmetadata c, manytomanydata m WHERE m.elementid='".$row->$field."' AND parentcode='".$row->documentcode."' AND m.relatedcode=c.documentcode";
							$this->db->executeQuery($query);
							$output = array();
							while($many = $this->db->nextObject()){
								array_push($output, $many->label);
							}
							$output = implode(';',$output);
						}else{
							$output = $row->$field;
						}
						
						array_push($displayrow, magicClean(str_replace('"', '""', trim($output)), false));
					}
					$displayrow = implode('","', $displayrow);
					$displayrow = '"' . $displayrow . '"'."\n";
		
					echo $displayrow;
				}
			}elseif($ext=='xml'){
				$sitetitle = TITLE;
				$httphost = CMS_HTTP_HOST;
				$formName = $this->formName;				
				echo <<< EOD

<!--
-
- $sitetitle XML Dump
- $httphost
-
-->

<!--
- Module : $formName
-->
<$sitetitle>
  <!-- Labels -->
    <ItemLabel>
EOD;
foreach ($this->customFields as $idx=>$field){
	echo "        <$field>".$this->customLabels[$idx]."</$field>\n";
}
				echo <<< EOD
    </ItemLabel>
  <!-- Items -->
EOD;
	$cmsTableWriter = new CmsTableWriter(CMS_FORMAT, $this->db);
	$cmsTableWriter->resetCustomIterator(" WHERE formid='$this->formId'");
	while ($row = $cmsTableWriter->nextItem()){
		echo "    <Item>";
		foreach($this->customFields as $field){
			
			$e = $this->formElements->getElementDescriptor($this->formDescription->$field);
			if($e->elementtype==CMS_ELEMENT_DROPDOWN){
				$dropDownTableWriter = new CmsTableWriter('cmscontentmetadata', $this->db);
				$dropDownTableWriter->resetCustomIterator(" WHERE documentcode='".$row->$field."' LIMIT 1");
				$dropDown = $dropDownTableWriter->nextItem();
				$output = $dropDown->label;
			}elseif($e->elementtype==CMS_ELEMENT_CHECKBOX_LIST){
				$query = "SELECT c.label FROM cmscontentmetadata c, manytomanydata m WHERE m.elementid='".$row->$field."' AND parentcode='".$row->documentcode."' AND m.relatedcode=c.documentcode";
				$this->db->executeQuery($query);
				$output = array();
				while($many = $this->db->nextObject()){
					array_push($output, $many->label);
				}
				$output = implode(';',$output);
			}else{
				$output = $row->$field;
			}			
			
			
			echo "        <$field>".magicClean(htmlspecialchars(trim($output)), false)."</$field>\n";
		}
		echo "    </Item>";
	}
				echo <<< EOD
</$sitetitle>
EOD;

			}
		}
		
		function outputBlankSpreadsheet(){
			
			header("Content-type: text/csv");
			header('Content-Disposition: attachment; filename="Spreadsheet-'.ucfirst($this->formName).'-template.csv"'."\n");
			
			$firstrow = implode('","', $this->availableLabels);
			$firstrow = '"' . $firstrow . '"'."\n";
			echo $firstrow;
		
		}
	}
?>
